home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 26 / Cream of the Crop 26.iso / os2 / pvm34b3.zip / pvm34b3 / pvm3 / gexamples / thb.c < prev    next >
C/C++ Source or Header  |  1997-07-22  |  3KB  |  127 lines

  1.  
  2. static char rcsid[] =
  3.     "$Id: thb.c,v 1.2 1997/07/09 13:28:01 pvmsrc Exp $";
  4.  
  5. /*
  6.  *         PVM version 3.4:  Parallel Virtual Machine System
  7.  *               University of Tennessee, Knoxville TN.
  8.  *           Oak Ridge National Laboratory, Oak Ridge TN.
  9.  *                   Emory University, Atlanta GA.
  10.  *      Authors:  J. J. Dongarra, G. E. Fagg, M. Fischer
  11.  *          G. A. Geist, J. A. Kohl, R. J. Manchek, P. Mucci,
  12.  *         P. M. Papadopoulos, S. L. Scott, and V. S. Sunderam
  13.  *                   (C) 1997 All Rights Reserved
  14.  *
  15.  *                              NOTICE
  16.  *
  17.  * Permission to use, copy, modify, and distribute this software and
  18.  * its documentation for any purpose and without fee is hereby granted
  19.  * provided that the above copyright notice appear in all copies and
  20.  * that both the copyright notice and this permission notice appear in
  21.  * supporting documentation.
  22.  *
  23.  * Neither the Institutions (Emory University, Oak Ridge National
  24.  * Laboratory, and University of Tennessee) nor the Authors make any
  25.  * representations about the suitability of this software for any
  26.  * purpose.  This software is provided ``as is'' without express or
  27.  * implied warranty.
  28.  *
  29.  * PVM version 3 was funded in part by the U.S. Department of Energy,
  30.  * the National Science Foundation and the State of Tennessee.
  31.  */
  32.  
  33. /*
  34.     Join a bunch of groups and do some barriers
  35. */
  36.  
  37. #include <stdio.h>
  38. #include "pvm3.h"
  39.  
  40. #define MAXGNAME 32 /* used to be a constant, length not important */
  41. #define MAXGTIDS 32 /* used to be a constant, Number not important */
  42. #define MAXNGROUPS 32 /* used to be constant, Number not important */
  43.  
  44. int
  45. main(argc, argv)
  46. int argc;
  47. char *argv[];
  48. {
  49.     int mytid, ctid[MAXGTIDS];
  50.     int nproc;
  51.     char g[MAXGNAME];
  52.     int cc;
  53.     int gid[MAXNGROUPS], gs;
  54.     int i;
  55.  
  56.     if (argc != 2) goto usage;
  57.     if ((nproc = atoi(argv[1])) < 1) goto usage;
  58.         
  59.     mytid = pvm_mytid();
  60.     if (mytid < 0) {
  61.         pvm_perror(argv[0]);
  62.         return -1;
  63.         }
  64.  
  65.     /* join a bunch of groups */
  66.     for (i = 0; i < MAXNGROUPS; i++) {
  67.         sprintf(g, "group%d", i);
  68.         if((gid[i] = pvm_joingroup(g)) < 0) {
  69.             pvm_perror("joining");
  70.             }
  71.         fprintf(stderr, "joined %s\n", g);
  72.         }
  73.  
  74.     /* test that i'm in the groups */
  75.     for (i = MAXNGROUPS-1; i  >= 0; i--) {
  76.         sprintf(g, "group%d", i);
  77.         if(gid[i] != (cc = pvm_getinst(g, mytid))) {
  78.             fprintf(stderr, "I'm not myself %d %s\n", cc, g);
  79.             pvm_perror("getinst");
  80.             }
  81.         if(mytid != (cc = pvm_gettid(g, gid[i]))) {
  82.             fprintf(stderr, "I'm not myself %d %s\n", cc, g);
  83.             pvm_perror("gettid");
  84.             }
  85.         }
  86.  
  87.     /* start a bunch of children */
  88.     printf("spawning ... ");
  89.     pvm_spawn("tnb", (char**)0, 0, "", nproc, ctid);
  90.  
  91.     for (i = 0; i < nproc; i++) {
  92.         if (ctid[i] < 0) {
  93.             fprintf(stderr, "trouble on spawn number %d\n", ctid[i]);
  94.             pvm_perror("th:");
  95.             }
  96.         else {
  97.             int foo;
  98.             foo = nproc+1;
  99.             /* send a group name to the child */
  100.             pvm_initsend(PvmDataDefault);
  101.             pvm_pkstr(g);
  102.             pvm_pkint(&foo, 1, 1);
  103.             pvm_send(ctid[i], 5);
  104.             }
  105.  
  106.         }
  107.  
  108.         printf ("barrier on %s \n", g);
  109.     pvm_barrier(g, nproc+1);
  110.     pvm_initsend(PvmDataDefault);
  111.     pvm_bcast(g, 77);
  112.  
  113.         printf(" leaving all the groups \n");
  114.     /* leave the groups */
  115.     for (i = 0; i < MAXNGROUPS; i++) {
  116.         sprintf(g, "group%d", i);
  117.         if(pvm_lvgroup(g) < 0)
  118.             pvm_perror("pvm_lvgroup");
  119.         }
  120.  
  121.     pvm_exit();
  122.     printf("done.\n");
  123.     return 0;
  124. usage:
  125.     fprintf(stderr, "usage: %s <nproc>\n", argv[0]);
  126. }
  127.